package com.btpj.module_main.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.btpj.module_main.data.entity.ProjectItemSub
import com.btpj.module_main.databinding.ListItemProjectBinding
import com.bumptech.glide.Glide

class NavPageAdapter constructor(val context: Context) :
    PagedListAdapter<ProjectItemSub, NavPageAdapter.ViewHolder>(object :
        DiffUtil.ItemCallback<ProjectItemSub>() {
        override fun areItemsTheSame(oldItem: ProjectItemSub, newItem: ProjectItemSub): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: ProjectItemSub, newItem: ProjectItemSub): Boolean {
            return oldItem == newItem
        }
    }) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ListItemProjectBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding.root, binding, context)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.setData(getItem(position))
    }

    class ViewHolder(
        itemView: View,
        private val binding: ListItemProjectBinding,
        val context: Context
    ) :
        RecyclerView.ViewHolder(itemView) {

        fun setData(item: ProjectItemSub?) {
            binding.itemData = item
            binding.context = context
            Glide.with(binding.ivPic).load(item!!.envelopePic).into(binding.ivPic)
        }
    }
}